package com.handydata.antifraud.rule.funcs;

import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import com.handydata.antifraud.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * 计算年龄
 */
@Component
public class CalcAge  extends CalculateAdapter {
    private static final Logger logger = LoggerFactory.getLogger(CalcAge.class);
    /**
     * 计算年龄
     * @param riskInputTO
     * @return
     */
    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO) {
        logger.info("{}开始计算年龄 idCard {}", RULE_CALC_TEXT,riskInputTO.getIdCard());

        String dateStr;
        String idCard = riskInputTO.getIdCard();
        if (idCard.length() == 15) {
            dateStr = "19" + idCard.substring(6, 12);
        } else if(idCard.length() == 18) {
            dateStr = idCard.substring(6, 14);
        } else {
            return;
        }
        LocalDate from = DateUtil.parse8(dateStr);
        int age = from.until(LocalDate.now()).getYears();
        riskMiddleTO.setAge(age);
        logger.info("{}结束计算年龄 {}", RULE_CALC_TEXT,age);
    }
}
